package design.mode.chapter3.item4.generator2;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.Random;

/**
 * 提高代码的可读性
 * hostName 变量不应该被重复使用，尤其当这两次使用时的含义还不同的时候；将获取 hostName 的代码抽离出来，定义为 getLastFieldOfHostName() 函数；
 * 删除代码中的魔法数，比如，57、90、97、122；
 * 将随机数生成的代码抽离出来，定义为 generateRandomAlphameric() 函数；
 * generate() 函数中的三个 if 逻辑重复了，且实现过于复杂，我们要对其进行简化；
 * 对 IdGenerator 类重命名，并且抽象出对应的接口。
 *
 * @author ZhongMingde on 2021/10/21
 */

public class RandomIdGenerator implements LogTraceIdGenerator {
    @Override
    public String generator() {
        String subStrOfHostName = getLastFieldOfHostName();
        long currentTimeMillis = System.currentTimeMillis();
        String randomString = generateRandomAlphameric(8);
        String id = String.format("%s-%d-%s", subStrOfHostName, currentTimeMillis, randomString);
        return id;
    }

    /**
     * 获取hostName的最后一位
     *
     * @return
     */
    private String getLastFieldOfHostName() {
        String subStrOfHostName = null;
        try {
            String hostName = Inet4Address.getLocalHost().getHostAddress();
            // String hostName = InetAddress.getLocalHost().getHostName();
            String[] tokens = hostName.split("\\.");
            subStrOfHostName = tokens[tokens.length - 1];
            return subStrOfHostName;
        } catch (UnknownHostException e) {
            // logger.warn("Failed to get the host name.", e);
        }
        return subStrOfHostName;
    }

    /**
     * 随机数生成
     *
     * @param length
     * @return
     */
    private String generateRandomAlphameric(int length) {
        char[] randomChars = new char[length];
        int count = 0;
        Random random = new Random();
        while (count < length) {
            int maxAscii = 'z';
            int randomAscii = random.nextInt(maxAscii);
            boolean isDigit = randomAscii >= '0' && randomAscii <= '9';
            boolean isUppercase = randomAscii >= 'A' && randomAscii <= 'Z';
            boolean isLowercase = randomAscii >= 'a' && randomAscii <= 'z';
            if (isDigit || isUppercase || isLowercase) {
                randomChars[count] = (char) (randomAscii);
                ++count;
            }
        }
        return new String(randomChars);
    }

}
